namespace Radar
{
    using System;

    internal sealed class SingletonLifetime : Lifetime
    {
        protected override Func<ResolveContext, object> GetBuilderInternal(Func<ResolveContext, object> initialBuild)
        {
            object result = null;
            var lockObject = new object();
            return c =>
                       {
                           if (result == null)
                           {
                               lock (lockObject)
                               {
                                   if (result == null)
                                       result = initialBuild(c);
                               }
                           }

                           return result;
                       };
        }
    }
}